﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Com.ProjectA.Utility;
using Com.ProjectA.BusinessLogic;
using Com.ProjectA.DAO;

namespace Com.ProjectA.RunTesting
{
    public class Program
    {
        public static void Main(string[] args)
        {
            string strPattern = @"\d";
            string strInput = @"jhdsi2uu*ie2^we.?";
            string strPattern1 = @"{\S\w*}";
            string strInput1 = @"select * from tb_test where id = {id} and name = {nm} or age > {12} or address like {nd}";
            string[] arrPerCd = new string[] { "01003004", "01003001", "01001001001002", "01001001001001001003", "01001001001001002002", "01001001001001002001"
                                             , "01001002", "01002001", "01002001001001", "01002001001002", "01001001"
                                             , "01002002001001", "01001001001001", "01001002001001", "01001001001001001002", "01001001001001001001", "01001001001002002001"};
            string strCurrentPerCd = "01001001001001001001";
            string strXFatherCdCorrect = "01001001001001";
            //string strXFatherCdWrong = "01001002001001";
            try
            {
                RegexUtil regexUtil = new RegexUtil(strPattern, System.Text.RegularExpressions.RegexOptions.ExplicitCapture);
                Console.WriteLine("test exists " + strInput);
                Console.WriteLine(regexUtil.CheckExists(strInput) ? "TRUE" : "FALSE");
                Console.WriteLine("test first matched string " + strInput);
                Console.WriteLine(String.IsNullOrWhiteSpace(regexUtil.FindMatchedString(strInput)) ? "NULL - EMPTY - WHITE" : regexUtil.FindMatchedString(strInput));
                Console.WriteLine("test matched string collection " + strInput);
                string[] strResult = regexUtil.FindMatchedStringArray(strInput);
                if (strResult != null && strResult.Length > 0)
                {
                    for (int i = 0; i < strResult.Length; i++)
                    {
                        Console.WriteLine(String.Format("string[{0}]: {1}", i, strResult[i]));
                    }
                }
                regexUtil = new RegexUtil(strPattern1, System.Text.RegularExpressions.RegexOptions.ExplicitCapture);
                string[] strResult1 = regexUtil.FindMatchedStringArray(strInput1);
                if (strResult1 != null && strResult1.Length > 0)
                {
                    for (int i = 0; i < strResult1.Length; i++)
                    {
                        Console.WriteLine(String.Format("string[{0}]: {1}", i, strResult1[i]));
                    }
                }
                Console.WriteLine(String.Format("List of family member: "));
                foreach (string per in arrPerCd)
                {
                    Console.WriteLine(String.Format("Person code: {0} - generation rank: {1}", per, BLGenealogic.FindGenRankOfPerCd(per)));
                    if (BLGenealogic.IsXFather(strCurrentPerCd, per, 1))
                    {
                        Console.WriteLine(String.Format("{0} is the father of {1}", per, strCurrentPerCd));
                    }
                    if (BLGenealogic.IsXFather(strCurrentPerCd, per, 2))
                    {
                        Console.WriteLine(String.Format("{0} is the grandfather of {1}", per, strCurrentPerCd));
                    }
                    if (BLGenealogic.IsXDescendant(strXFatherCdCorrect, per, 1))
                    {
                        Console.WriteLine(String.Format("{0} is the child of {1}", per, strXFatherCdCorrect));
                    }
                    if (BLGenealogic.CheckIsBrother(strCurrentPerCd, per, true)) 
                    {
                        Console.WriteLine(String.Format("{0} is the younger bro of {1}", per, strCurrentPerCd));
                    }
                    if (BLGenealogic.CheckIsBrother(strCurrentPerCd, per, false))
                    {
                        Console.WriteLine(String.Format("{0} is the older bro of {1}", per, strCurrentPerCd));
                    }
                }
                #region INSERT DATA TO DB
                //new BLTest().InsertTableTest(new DAOTest() { id = 1, name = "Timbake", address = "le van sy", status = "Y"});
                #endregion INSERT DATA TO DB

                #region REFLECTION
                DateTime dateTime = new DateTime();
                int intTest = 23;
                string strTest = "timbake";
                Console.WriteLine("Value of DateTime: " + ConvertObjToCorrectType(dateTime).ToString());
                Console.WriteLine("Value of Integer: " + ConvertObjToCorrectType(intTest).ToString());
                Console.WriteLine("Value of String: " + ConvertObjToCorrectType(strTest).ToString());
                #endregion REFLECTION
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            finally
            {
                Console.Read();
            }
        }

        private static object ConvertObjToCorrectType<T>(T objInput)
        {
            try
            {
                if (typeof(T) == typeof(System.DateTime))
                {
                    return objInput == null ? new DateTime() : Convert.ToDateTime(objInput);
                }
                else if (typeof(T) == typeof(System.String))
                {
                    return objInput == null ? String.Empty : objInput.ToString();
                }
                else if (typeof(T) == typeof(System.Int32))
                {
                    return objInput == null ? 0 : Convert.ToInt32(objInput.ToString());
                }
                else
                {
                    return default(T);
                }
            }
            catch (Exception ex)
            {
                return default(T);
            }
        }
    }
}
